﻿import tkinter as tk
import random
import threading
import time
import math
import ctypes

close_event = threading.Event()
SCREEN_W, SCREEN_H = 0, 0

def get_screen_size():
    user32 = ctypes.windll.user32
    user32.SetProcessDPIAware()
    width = user32.GetSystemMetrics(0)
    height = user32.GetSystemMetrics(1)
    return width, height

def generate_heart_points(num_points, window_width, window_height):
    screen_w, screen_h = get_screen_size()
    margin_x = max(20, window_width // 2)
    margin_y = max(20, window_height // 2)
    usable_w = max(1, screen_w - 2 * margin_x - window_width)
    usable_h = max(1, screen_h - 2 * margin_y - window_height)
    
    points = []
    for i in range(num_points):
        t = 2 * math.pi * i / num_points
        x0 = 16 * math.sin(t) ** 3
        y0 = 13 * math.cos(t) - 5 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t)
        points.append((x0, y0))
    
    xs = [p[0] for p in points]
    ys = [p[1] for p in points]
    min_x, max_x = min(xs), max(xs)
    min_y, max_y = min(ys), max(ys)
    span_x = max(1e-6, max_x - min_x)
    span_y = max(1e-6, max_y - min_y)
    
    mapped = []
    for x0, y0 in points:
        nx = (x0 - min_x) / span_x
        ny = (y0 - min_y) / span_y
        px = int(margin_x + nx * usable_w)
        py = int(margin_y + (1 - ny) * usable_h)
        px = max(0, min(px, screen_w - window_width))
        py = max(0, min(py, screen_h - window_height))
        mapped.append((px, py))
    
    dedup = []
    seen = set()
    for p in mapped:
        if p not in seen:
            seen.add(p)
            dedup.append(p)
    return dedup

def show_warn_tip(x, y, window_width=250, window_height=60):
    window = tk.Tk()
    window.title('温馨提示')
    window.geometry(f"{window_width}x{window_height}+{x}+{y}")
    
    tips = ['多喝水哦~', '保持微笑呀', '每天都要元气满满', '记得吃水果', '保持好心情', '好好爱自己', '我想你了',
            '梦想成真', '期待下一次见面', '天冷了，多穿衣服', '愿所有烦恼都消失', '保持微笑呀', '别熬夜', '爱你哦~']
    tip = random.choice(tips)
    
    bg_colors = ['lightpink', 'skyblue', 'lightgreen', 'lavender', 'lightyellow', 'plum', 'coral', 'bisque',
                 'aquamarine', 'mistyrose', 'honeydew']
    bg = random.choice(bg_colors)
    
    tk.Label(window, text=tip, bg=bg, font=('微软雅黑', 16), width=30, height=3).pack()
    window.attributes('-topmost', True)
    
    # 绑定按键事件，按下ESC键关闭所有窗口
    window.bind('<Escape>', lambda e: close_event.set())
    window.bind('<Key>', lambda e: close_event.set() if e.keysym == 'Escape' else None)
    
    def poll_close():
        if close_event.is_set():
            window.destroy()
            return
        window.after(200, poll_close)
    window.after(200, poll_close)
    window.mainloop()

def main():
    num_points = 100  # 原来是1000，改为100增加窗口间距
    window_width, window_height = 250, 60
    points = generate_heart_points(num_points, window_width, window_height)
    
    # 创建一个线程用于监听键盘输入
    def listen_for_exit():
        input("按回车键关闭所有窗口...\n")
        close_event.set()
    
    listener_thread = threading.Thread(target=listen_for_exit, daemon=True)
    listener_thread.start()
    
    for x, y in points:
        t = threading.Thread(target=show_warn_tip, args=(x, y, window_width, window_height))
        t.daemon = True
        t.start()
        time.sleep(0.05)  # 控制窗口生成速度，避免卡顿

    # 等待用户输入回车键
    listener_thread.join()

if __name__ == "__main__":
    main()